from PIL import Image
import numpy as np
from numpy import fft
from tkinter import Tk
from tkinter.filedialog import askopenfilename
import json
from functools import reduce
import operator

Tk().withdraw() # we don't want a full GUI, so keep the root window from appearing
filename = askopenfilename() # show an "Open" dialog box and return the path to the selected file
img = Image.open(filename)
sampleSpace = img.convert('L')
sampleSpace = np.array([1000] * (4 ** 2)).reshape(4, 4)
sampleFrequency = fft.fft2(sampleSpace)

imgFreqReal = Image.fromarray(np.real(sampleFrequency))
imgFreqReal.show()
imgFreqImag = Image.fromarray(np.imag(sampleFrequency))
imgFreqImag.show()

sampleSpace = fft.ifft2(sampleFrequency)
imgSpaceReal = Image.fromarray(np.real(sampleSpace))
imgSpaceReal.show()

# json = json.dumps({
#     'realarray': reduce(operator.add, np.real(sampleFrequency).tolist(), []),
#     'imagarray': reduce(operator.add, np.imag(sampleFrequency).tolist(), []),
# })
# with open('lena.json', 'w') as file:
# 	file.write(json)
